﻿namespace WMS6.Web.UserControls
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class UDFReportCtrl : UserControl
    {
        private string cssclass = "tLotAttribute";
        private TableRow ctrltr = null;
        protected HtmlGenericControl DivCtrl;
        private string heigth = "100%";
        private List<string> IsMustInput = new List<string>();
        protected Label lblmsg;
        private TableRow lbltr = null;
        private int ListCount = 0;
        private string row = "1";
        private Table table = null;
        private int trRow;
        private string width = "100%";

        private void ClearCtrl()
        {
            this.table = null;
            this.lbltr = null;
            this.ctrltr = null;
            this.trRow = 0;
            this.DivCtrl.InnerHtml = "";
            base.Session["UdfReportCtrl"] = null;
            this.lblmsg.Text = "";
        }

        private void GenerateCtrl(List<UdfGridReportSettingDetailInfo> UdfReportSettingDetailList)
        {
            if (UdfReportSettingDetailList != null)
            {
                this.ListCount = UdfReportSettingDetailList.Count;
                foreach (UdfGridReportSettingDetailInfo info in UdfReportSettingDetailList)
                {
                    TextBox txtbox;
                    List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> LotAttributeCode;
                    this.ListCount--;
                    TableCell lbltd = new TableCell();
                    TableCell ctrltd = new TableCell();
                    Label lblCtrl = new Label {
                        ID = "lbl" + info.CtrlID,
                        Text = info.CtrlLabel
                    };
                    lbltd.Controls.Add(lblCtrl);
                    if (info.IsMustInput == "Y")
                    {
                        Label lblMustInput = new Label {
                            Text = "*",
                            ForeColor = Color.Red
                        };
                        lbltd.Controls.Add(lblMustInput);
                    }
                    string CS40002 = info.CtrlType;
                    if (CS40002 != null)
                    {
                        if (!(CS40002 == "DATETIME"))
                        {
                            if (CS40002 == "TEXTBOX")
                            {
                                goto Label_0240;
                            }
                            if (CS40002 == "DROPDOWNLIST")
                            {
                                goto Label_02C5;
                            }
                        }
                        else
                        {
                            TextBox txtboxDateTime = new TextBox {
                                ID = info.CtrlID,
                                Text = string.IsNullOrEmpty(info.Udf1) ? info.DefaultValue : info.Udf1,
                                CssClass = "textbox"
                            };
                            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image {
                                ID = "img" + info.CtrlID,
                                ImageUrl = "../Images/calendar.gif",
                                CssClass = "img",
                                AlternateText = "请选择日期"
                            };
                            img.Attributes.Add("onclick", "setday(this,'" + this.ClientID + "_" + txtboxDateTime.ClientID.ToString() + "')");
                            ctrltd.Controls.Add(txtboxDateTime);
                            ctrltd.Controls.Add(img);
                            if (info.IsMustInput == "Y")
                            {
                                this.IsMustInput.Add(info.CtrlID);
                            }
                        }
                    }
                    goto Label_039A;
                Label_0240:
                    txtbox = new TextBox();
                    txtbox.ID = info.CtrlID;
                    txtbox.Text = string.IsNullOrEmpty(info.Udf1) ? info.DefaultValue : info.Udf1;
                    txtbox.CssClass = "textbox";
                    ctrltd.Controls.Add(txtbox);
                    if (info.IsMustInput == "Y")
                    {
                        this.IsMustInput.Add(info.CtrlID);
                    }
                    goto Label_039A;
                Label_02C5:
                    LotAttributeCode = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource(info.DefaultValue);
                    DropDownList ddl = new DropDownList {
                        ID = info.CtrlID,
                        DataSource = LotAttributeCode,
                        CssClass = "mediumselectbox",
                        DataTextField = "SysCodeText",
                        DataValueField = "SysCodeValue"
                    };
                    ddl.DataBind();
                    if (!string.IsNullOrEmpty(info.Udf1))
                    {
                        ddl.SelectedValue = info.Udf1;
                    }
                    if (info.IsMustInput == "N")
                    {
                        ddl.Items.Insert(0, "");
                    }
                    else
                    {
                        this.IsMustInput.Add(info.CtrlID);
                    }
                    ctrltd.Controls.Add(ddl);
                Label_039A:
                    this.RecordHtml(lbltd, ctrltd);
                }
                this.DivCtrl.Controls.Add(this.table);
                base.Session["UdfTable"] = this.table;
            }
        }

        private void GenerateCtrl(List<UdfReportSettingDetailInfo> UdfReportSettingDetailList)
        {
            if (UdfReportSettingDetailList != null)
            {
                this.ListCount = UdfReportSettingDetailList.Count;
                foreach (UdfReportSettingDetailInfo info in UdfReportSettingDetailList)
                {
                    TextBox txtbox;
                    List<WMS6.Web.CommonMethodServiceRef.SysCode4DDL> LotAttributeCode;
                    this.ListCount--;
                    TableCell lbltd = new TableCell();
                    TableCell ctrltd = new TableCell();
                    Label lblCtrl = new Label {
                        ID = "lbl" + info.CtrlID,
                        Text = info.CtrlLable
                    };
                    lbltd.Controls.Add(lblCtrl);
                    if (info.IsMustInput == "Y")
                    {
                        Label lblMustInput = new Label {
                            Text = "*",
                            ForeColor = Color.Red
                        };
                        lbltd.Controls.Add(lblMustInput);
                    }
                    string CS40002 = info.CtrlType;
                    if (CS40002 != null)
                    {
                        if (!(CS40002 == "DATETIME"))
                        {
                            if (CS40002 == "TEXTBOX")
                            {
                                goto Label_0240;
                            }
                            if (CS40002 == "DROPDOWNLIST")
                            {
                                goto Label_02C5;
                            }
                        }
                        else
                        {
                            TextBox txtboxDateTime = new TextBox {
                                ID = info.CtrlID,
                                Text = string.IsNullOrEmpty(info.Udf1) ? info.DefaultValue : info.Udf1,
                                CssClass = "textbox"
                            };
                            System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image {
                                ID = "img" + info.CtrlID,
                                ImageUrl = "../Images/calendar.gif",
                                CssClass = "img",
                                AlternateText = "请选择日期"
                            };
                            img.Attributes.Add("onclick", "setday(this,'" + this.ClientID + "_" + txtboxDateTime.ClientID.ToString() + "')");
                            ctrltd.Controls.Add(txtboxDateTime);
                            ctrltd.Controls.Add(img);
                            if (info.IsMustInput == "Y")
                            {
                                this.IsMustInput.Add(info.CtrlID);
                            }
                        }
                    }
                    goto Label_039A;
                Label_0240:
                    txtbox = new TextBox();
                    txtbox.ID = info.CtrlID;
                    txtbox.Text = string.IsNullOrEmpty(info.Udf1) ? info.DefaultValue : info.Udf1;
                    txtbox.CssClass = "textbox";
                    ctrltd.Controls.Add(txtbox);
                    if (info.IsMustInput == "Y")
                    {
                        this.IsMustInput.Add(info.CtrlID);
                    }
                    goto Label_039A;
                Label_02C5:
                    LotAttributeCode = CommoneMethodServiceClientProxy.GetInstance().GetSysCodeDataSource(info.DefaultValue);
                    DropDownList ddl = new DropDownList {
                        ID = info.CtrlID,
                        DataSource = LotAttributeCode,
                        CssClass = "mediumselectbox",
                        DataTextField = "SysCodeText",
                        DataValueField = "SysCodeValue"
                    };
                    ddl.DataBind();
                    if (!string.IsNullOrEmpty(info.Udf1))
                    {
                        ddl.SelectedValue = info.Udf1;
                    }
                    if (info.IsMustInput == "N")
                    {
                        ddl.Items.Insert(0, "");
                    }
                    else
                    {
                        this.IsMustInput.Add(info.CtrlID);
                    }
                    ctrltd.Controls.Add(ddl);
                Label_039A:
                    this.RecordHtml(lbltd, ctrltd);
                }
                this.DivCtrl.Controls.Add(this.table);
                base.Session["UdfTable"] = this.table;
            }
        }

        public List<UdfGridReportSettingDetailInfo> GetUdfGridReoprtCtrlValue()
        {
            List<UdfGridReportSettingDetailInfo> udfReportSettingDetailList = null;
            string strIsMustInput = string.Empty;
            udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfGridReportSettingDetailInfo>;
            if ((udfReportSettingDetailList != null) && (udfReportSettingDetailList.Count<UdfGridReportSettingDetailInfo>() > 0))
            {
                foreach (UdfGridReportSettingDetailInfo info in udfReportSettingDetailList)
                {
                    info.Udf1 = base.Request.Form[this.ClientID + "$" + info.CtrlID].ToString();
                    if (((info.IsMustInput == "Y") && string.IsNullOrEmpty(info.Udf1)) && string.IsNullOrEmpty(strIsMustInput))
                    {
                        strIsMustInput = info.CtrlLabel;
                    }
                }
                base.Session["UdfReportCtrl"] = udfReportSettingDetailList;
                this.GenerateCtrl(udfReportSettingDetailList);
                if (!string.IsNullOrEmpty(strIsMustInput))
                {
                    this.lblmsg.Text = strIsMustInput + " 不能为空!";
                    return null;
                }
                this.lblmsg.Text = "";
                return udfReportSettingDetailList;
            }
            base.Session["UdfReportCtrl"] = new List<UdfGridReportSettingDetailInfo>();
            return udfReportSettingDetailList;
        }

        public List<UdfReportSettingDetailInfo> GetUdfReoprtCtrlValue()
        {
            List<UdfReportSettingDetailInfo> udfReportSettingDetailList = null;
            string strIsMustInput = string.Empty;
            udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfReportSettingDetailInfo>;
            if ((udfReportSettingDetailList != null) && (udfReportSettingDetailList.Count<UdfReportSettingDetailInfo>() > 0))
            {
                foreach (UdfReportSettingDetailInfo info in udfReportSettingDetailList)
                {
                    info.Udf1 = base.Request.Form[this.ClientID + "$" + info.CtrlID].ToString();
                    if (((info.IsMustInput == "Y") && string.IsNullOrEmpty(info.Udf1)) && string.IsNullOrEmpty(strIsMustInput))
                    {
                        strIsMustInput = info.CtrlLable;
                    }
                }
                base.Session["UdfReportCtrl"] = udfReportSettingDetailList;
                this.GenerateCtrl(udfReportSettingDetailList);
                if (!string.IsNullOrEmpty(strIsMustInput))
                {
                    this.lblmsg.Text = strIsMustInput + " 不能为空!";
                    return null;
                }
                return udfReportSettingDetailList;
            }
            base.Session["UdfReportCtrl"] = new List<UdfReportSettingDetailInfo>();
            return udfReportSettingDetailList;
        }

        private bool GetUdfReportCtrlDataSourceByUdfReprotID(string UdfReportID, string udfType)
        {
            List<object> objList;
            if (udfType == "UDF_REPORT_SETTING")
            {
                objList = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoListByID(UdfReportID, "", "UDF_REPORT_SETTING_DETAIL");
                if (objList != null)
                {
                    List<UdfReportSettingDetailInfo> UdfReportSettingDetailInfoList = new List<UdfReportSettingDetailInfo>();
                    foreach (object info in objList)
                    {
                        UdfReportSettingDetailInfoList.Add(info as UdfReportSettingDetailInfo);
                    }
                    base.Session["UdfReportCtrl"] = null;
                    base.Session["UdfReportCtrl"] = UdfReportSettingDetailInfoList;
                }
            }
            if (udfType == "UDF_GRID_REPORT_SETTING")
            {
                objList = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoListByID(UdfReportID, "", "UDF_GRID_REPORT_SETTING_DETAIL");
                if (objList != null)
                {
                    List<UdfGridReportSettingDetailInfo> UdfReportSettingDetailInfoList = new List<UdfGridReportSettingDetailInfo>();
                    foreach (object info in objList)
                    {
                        UdfReportSettingDetailInfoList.Add(info as UdfGridReportSettingDetailInfo);
                    }
                    base.Session["UdfReportCtrl"] = null;
                    base.Session["UdfReportCtrl"] = UdfReportSettingDetailInfoList;
                }
            }
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Response.Buffer = false;
            this.Page.Response.Cache.SetNoStore();
        }

        private void RecordHtml(TableCell lbltd, TableCell ctrtd)
        {
            if (this.table == null)
            {
                this.table = new Table();
                this.table.ID = "UdfCtrl";
                this.table.Width = Unit.Parse(this.Width);
                this.table.Height = Unit.Parse(this.Heigth);
                this.table.CssClass = this.CssClass;
            }
            if (this.lbltr == null)
            {
                this.lbltr = new TableRow();
                this.ctrltr = new TableRow();
                this.trRow = int.Parse(this.Row);
            }
            this.lbltr.Controls.Add(lbltd);
            this.ctrltr.Controls.Add(ctrtd);
            if (this.ListCount == 0)
            {
                this.trRow = 0;
            }
            else
            {
                this.trRow--;
            }
            if (this.trRow == 0)
            {
                this.table.Controls.Add(this.lbltr);
                this.table.Controls.Add(this.ctrltr);
                this.lbltr = new TableRow();
                this.ctrltr = new TableRow();
                this.trRow = int.Parse(this.Row);
            }
        }

        public void UdfReportCtrlLoad(string UdfReportID, string udfType)
        {
            if (udfType == "UDF_REPORT_SETTING")
            {
                this.ClearCtrl();
                List<UdfReportSettingDetailInfo> udfReportSettingDetailList = new List<UdfReportSettingDetailInfo>();
                if (base.Session["UdfReportCtrl"] == null)
                {
                    if (this.GetUdfReportCtrlDataSourceByUdfReprotID(UdfReportID, udfType))
                    {
                        udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfReportSettingDetailInfo>;
                        this.GenerateCtrl(udfReportSettingDetailList);
                    }
                }
                else
                {
                    udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfReportSettingDetailInfo>;
                    this.GenerateCtrl(udfReportSettingDetailList);
                }
            }
            if (udfType == "UDF_GRID_REPORT_SETTING")
            {
                this.ClearCtrl();
                List<UdfGridReportSettingDetailInfo> udfReportSettingDetailList = new List<UdfGridReportSettingDetailInfo>();
                if (base.Session["UdfReportCtrl"] == null)
                {
                    if (this.GetUdfReportCtrlDataSourceByUdfReprotID(UdfReportID, udfType))
                    {
                        udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfGridReportSettingDetailInfo>;
                        this.GenerateCtrl(udfReportSettingDetailList);
                    }
                }
                else
                {
                    udfReportSettingDetailList = base.Session["UdfReportCtrl"] as List<UdfGridReportSettingDetailInfo>;
                    this.GenerateCtrl(udfReportSettingDetailList);
                }
            }
        }

        public string CssClass
        {
            get
            {
                return this.cssclass;
            }
            set
            {
                this.cssclass = value;
            }
        }

        public string Heigth
        {
            get
            {
                return this.heigth;
            }
            set
            {
                this.heigth = value;
            }
        }

        public string Row
        {
            get
            {
                return this.row;
            }
            set
            {
                this.row = value;
            }
        }

        public string Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
            }
        }
    }
}

